gun_violence_df <- read_csv("gun-violence-data_01-2013_03-2018.csv")
## Rows: 239677 Columns: 29
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (18): state, city_or_county, address, incident_url, source_url, gun_sto...
## dbl (9): incident_id, n_killed, n_injured, congressional_district, latitud...
## lgl (1): incident_url_fields_missing
## date (1): date
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(gun_violence_df)
## # A tibble: 6 × 29
## incident_id date state city_or_county address n_killed n_injured
## <dbl> <date> <chr> <chr> <chr> <dbl> <dbl>
## 1 461105 2013-01-01 Pennsylvania Mckeesport 1506 V… 0 4
## 2 460726 2013-01-01 California Hawthorne 13500 … 1 3
## 3 478855 2013-01-01 Ohio Lorain 1776 E… 1 3
## 4 478925 2013-01-05 Colorado Aurora 16000 … 4 0
## 5 478959 2013-01-07 North Caroli… Greensboro 307 Mo… 2 2
## 6 478948 2013-01-07 Oklahoma Tulsa 6000 b… 4 0
## # ℹ 22 more variables: incident_url <chr>, source_url <chr>,
## # incident_url_fields_missing <lgl>, congressional_district <dbl>,
## # gun_stolen <chr>, gun_type <chr>, incident_characteristics <chr>,
## # latitude <dbl>, location_description <chr>, longitude <dbl>,
## # n_guns_involved <dbl>, notes <chr>, participant_age <chr>,
## # participant_age_group <chr>, participant_gender <chr>,
## # participant_name <chr>, participant_relationship <chr>, …
gun_violence_df <- subset(gun_violence_df, select = -c(address,incident_url, source_url,incident_url_fields_missing,congressional_district, location_description, notes, n_guns_involved,participant_name,sources, state_house_district, state_senate_district,participant_relationship))
Q. What are the geographic patterns of gun violence incidents in the United States? Are certain states, cities, or neighborhoods more likely to experience incidents?
unique_values <- table(gun_violence_df$state)
death_injured_count <- data.frame(unique_values)
total_killed <- aggregate(cbind(gun_violence_df$n_killed, gun_violence_df$n_injured) ~ gun_violence_df$state, data = gun_violence_df, FUN = sum)
total_killed <- total_killed %>%
rename("states" = "gun_violence_df$state",
"total_killed" = "V1",
"total_injured" = "V2")
total_killed$total_cases <- total_killed$total_killed + total_killed$total_injured
my_sf_layer_wgs84 <- inner_join(my_sf_layer_wgs84,total_killed, by = c("NAME" = "states"))
pal2 <- colorNumeric("plasma", domain = my_sf_layer_wgs84$total_cases, reverse = TRUE)
labels <- sprintf(
"<strong>State: %s</strong><br/><strong>Total Injured: </strong>%s<br/><strong>Total Killed: </strong>%s</sup>",
my_sf_layer_wgs84$NAME, my_sf_layer_wgs84$total_injured, my_sf_layer_wgs84$total_killed) %>% lapply(htmltools::HTML)
leaflet(my_sf_layer_wgs84) %>%
addTiles() %>%
addPolygons(fillColor = ~pal2(my_sf_layer_wgs84$total_cases),
fillOpacity = .5,
color = "black",
weight = 0.4,
label = labels,
dashArray = "5",
highlightOptions = highlightOptions(
weight = 8,
color = "skyblue",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE),
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "10px",
direction = "auto")) %>%
addLegend('topright',
pal = pal2,
values =~my_sf_layer_wgs84$total_cases,
title = 'Gun Violence in US(2013-18)')%>%
addMiniMap(
tiles = providers$Esri.WorldStreetMap,
toggleDisplay = TRUE
)%>%
addEasyButton(easyButton(
icon="fa-globe", title="Zoom to Level 1",
onClick=JS("function(btn, map){ map.setZoom(2); }")))